// read gene list from bed format file. refgene hg19 is used
#ifndef __GENEBEDREADER__
#define __GENEBEDREADER__

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

class GeneField{
	// chr 
	unsigned short chr;
	// original chr info in file
	string chr_f;
	string strand;
	unsigned int start;
	unsigned int end;
	string name;
public:
	GeneField(string input){
		istringstream iss(input);
		iss >> chr_f >> start >> end >> strand >> name;
		string temp;
		if (chr_f.find("chr")!=string::npos){
			// return the rest of the string following chr
			temp = chr_f.substr(3);
		}
		if (temp.find("random")!=string::npos || temp.find("Un")!=string::npos){
			temp = "0";
		} else if (temp == "X"){
			temp = "23";
		} else if (temp == "Y"){
			temp = "25";
		} else if (temp.find("mito")!=string::npos){
			temp = "26";
		}
		chr = atoi(temp.c_str());
		if (chr > 26){
			fprintf(stderr,"illegal chr value: %d",chr);
			exit(1);
		}
	}
	const unsigned short &get_chr() const{
		return chr;
	}
	const unsigned int &get_start() const{
		return start;
	}
	const unsigned int &get_end() const{
		return end;
	}
	const string &get_genename() const{
		return name;
	}
};

class GeneFileReader{
private:
	char* filename;
	vector<GeneField*> genevec;
public:
	// constructor opens file
	GeneFileReader(char* str);
	const string get_filename() const{
		return filename;
	}
	const vector<GeneField*>& get_genevec() const{
		return genevec;
	}
	struct genevec_comp{
		bool operator()(GeneField const* a, GeneField const* b){
			if (a->get_chr()  < b->get_chr()) {
				return true;
			} else if (a->get_chr() > b->get_chr()){
				return false;
			} else {
				if (a->get_start() < b->get_start()){
					return true;
				}
				else {
					return false;
				}
			}
		}
	};

};

#endif